package com.kin.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kin.service.IPageService;
import com.kin.service.IRoleAllService;
import com.kin.service.IRoleService;
import com.kin.service.IUserPermissionService;
import com.kin.service.IUserService;
import com.kin.tools.IToolsService;

/**
 * 数据获取与更新Controller
 * @author lauvinson
 * @time 2017年7月14日08:43:53
 */
@Controller("dataController")
public class DataController {
	
	private Logger logger = Logger.getLogger(this.getClass());
	

	@Resource
	private IRoleService roleService;//单一角色权限Service
	
	@Resource
	private IRoleAllService roleAllService;//所有角色权限Service
	
	@Resource
	private IToolsService toolsService;//工具Service
	
	@Resource
	private IUserService userService;//用户Service
	
	@Resource
	private IPageService pageService;//页面Service
	
	@Resource
	private IUserPermissionService userPermissionService;//用户私有权限Service
	
	/**
	 * 获取所有权限信息和分页数(POST)
	 * @param page_info (json)(pstart,psize)
	 * @return (json)roleAll,(json)count
	 */
	@RequestMapping(value="/userRole",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject userRole(@RequestBody JSONObject page_info)
	{
		int pstart = Integer.parseInt(page_info.getString("pstart"));
		int psize = Integer.parseInt(page_info.getString("psize"));
		int count = roleAllService.getCount()%psize == 0 ?roleAllService.getCount()/psize : roleAllService.getCount()/psize+1;
		System.out.println("pageNum"+count);
		JSONObject roleJ = toolsService.stringToJsonObject(JSON.toJSONString(roleAllService.selectAll(pstart,psize)));
		roleJ.put("count", count);
		return roleJ;
	}
	
	
	/**
	 * 变更组权限信息(POST)
	 * @param update_info (json)(userid,pageid)
	 * @return status:success，status:fail
	 */
	@RequestMapping (value="/updateRole",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateRole(@RequestBody JSONObject update_info)
	{
		try{
		int userid = Integer.parseInt(update_info.getString("userid"));
		int pageid = Integer.parseInt(update_info.getString("pageid"));
		int state;
		int groupid = userService.getUserById(userid).getGroupid();
		System.out.println(userid+"=="+pageid+"=="+groupid);
		
		 if(roleService.getPermission(userid, update_info.getString("url").trim()) == 1)
	        {
	        	state = 0;
	        }else{state = 1;}
		
		int status = roleService.updateByGroupidAndPageid(groupid, pageid,state);
		System.out.println(state+"---------------"+status);
		if(status == 1)
		{
			return toolsService.stringToJsonObject(UserController.STATUS_SUCCESS);
		}
		}
		catch(Exception e){
			e.printStackTrace();
			return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
		}
		return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
		
	}
	
	
	
	/**
	 * 变更用户私有权限(POST)
	 * @param update_info json(userid,pageid)
	 * @return status:success，status:fail
	 */
	@RequestMapping (value="/updateUserRole",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateUserRole(@RequestBody JSONObject update_info)
	{
		try{
			int userid = Integer.parseInt(update_info.getString("userid"));
			int pageid = Integer.parseInt(update_info.getString("pageid"));
			int state =1;
			System.out.println(userid+"=="+pageid);
			
			if(userPermissionService.getUserPermissionByUserIdAndPageId(userid, pageid)!=null){
					if(userPermissionService.getUserPermissionByUserIdAndPageId(userid, pageid).getState() == 1)
			        {
			        	state = 0;
			        }
			}else
			{
				userPermissionService.insertUserPermission(userid, pageid, state);
			}
			
				int status = userPermissionService.updatePermissionByUserID(userid, pageid, state);
				System.out.println(state+"---------------"+status);
				if(status == 1)
				{
					return toolsService.stringToJsonObject(UserController.STATUS_SUCCESS);
				}
				}
				catch(Exception e){
					e.printStackTrace();
					return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
				}
				return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
		
	}
	
	/**
	 * 通过userid和来改变用户的groupid
	 * @param updateUserGroupId_Info json(userid,groupid)
	 * @return
	 */
	@RequestMapping("updateUserGroupId")
	@ResponseBody
	public JSONObject updateUserGroupID(@RequestBody JSONObject updateUserGroupId_Info)
	{
		int userid = Integer.parseInt(updateUserGroupId_Info.getString("userid"));
		int groupid = Integer.parseInt(updateUserGroupId_Info.getString("groupid"));
		try
		{
			if(userService.updateUserGroupIdById(userid, groupid)==1)
			{
				return toolsService.stringToJsonObject(UserController.STATUS_SUCCESS);
			}
			else
			{
				return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
			}
		}
		catch(Exception e)
		{
			logger.info(e.getMessage());
			return toolsService.stringToJsonObject(UserController.STATUS_ERROR);
		}
	}
	
}
